-
Notifications
You must be signed in to change notification settings - Fork 92
gw-user-registration-prevent-populating-mapped-fields.php: Added a snippet to prevent mapped fields from being populated with saved values from the User Registration feed.
#1065
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
gw-user-registration-prevent-populating-mapped-fields.php: Added a snippet to prevent mapped fields from being populated with saved values from the User Registration feed.
#1065
Conversation
WalkthroughA new PHP file has been introduced to the Gravity Forms User Registration add-on that implements functionality to prevent the population of certain mapped fields with saved values. The code adds an action hook to Changes
Sequence Diagram(s)sequenceDiagram
participant GF as Gravity Forms
participant Func as Prevent Population Function
GF->>Func: Trigger gform_user_registration_user_data_pre_populate(mapped_fields, form, feed)
alt Form ID equals 1
Func->>Func: Set mapped_fields[3] = empty string
else Form ID does not equal 1
Func->>Func: Return mapped_fields unchanged
end
Func->>GF: Return modified mapped_fields
Suggested reviewers
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (4)
gravity-forms/gw-user-registration-prevent-populating-mapped-fields.php (4)
1-7: Documentation is clear but contains slight repetition.The header documentation clearly explains the purpose of the snippet. There's a minor repetition in line 6 where "prevent the population of mapped fields with the saved values" appears twice in the description.
/** * Gravity Wiz // Gravity Forms User Registration // Prevent Population Of Mapped Fields With The Saved Values * https://gravitywiz.com/ * - * Prevent population of mapped fields with the saved values. This snippet prevent the population of mapped fields with the saved values while displaying the form. + * This snippet prevents the population of mapped fields with saved values when displaying the form. */
8-12: Consider making the form ID configurable.The code uses a hardcoded form ID, which requires manual editing for different use cases. While the comment does instruct users to replace this value, consider making it more configurable (e.g., through a constant or by accepting an array of form IDs).
+ // Define the form ID(s) where you want to prevent populating mapped fields + $target_form_ids = array( 1 ); + add_action( 'gform_user_registration_user_data_pre_populate', function ( $mapped_fields, $form, $feed ) { - // Replace "1" with the form ID you want to prevent the population of mapped fields. - if ( $form['id'] !== 1 ) { + // Access the global variable containing target form IDs + global $target_form_ids; + // Check if current form should have mapped fields prevented + if ( ! in_array( $form['id'], $target_form_ids ) ) { return $mapped_fields; }
14-15: Consider supporting multiple field IDs.Similar to the form ID, the field ID is hardcoded. Consider enhancing the snippet to support multiple field IDs that should be emptied.
- // Replace "3" with the field ID you want to display empty. - $mapped_fields[3] = ''; + // Define the field IDs you want to display empty + $target_field_ids = array( 3 ); + + foreach ( $target_field_ids as $field_id ) { + if ( isset( $mapped_fields[$field_id] ) ) { + $mapped_fields[$field_id] = ''; + } + }
8-21: Overall functionality looks correct.The implementation correctly hooks into the
gform_user_registration_user_data_pre_populateaction to prevent populating mapped fields with saved values. The early return pattern is used effectively to only modify the targeted form.However, I'd recommend adding some minimal error handling or validation, such as checking if the form and feed arrays are properly structured before accessing their properties.
add_action( 'gform_user_registration_user_data_pre_populate', function ( $mapped_fields, $form, $feed ) { + // Validate input parameters + if ( ! is_array( $form ) || ! isset( $form['id'] ) || ! is_array( $mapped_fields ) ) { + return $mapped_fields; + } + // Replace "1" with the form ID you want to prevent the population of mapped fields. if ( $form['id'] !== 1 ) { return $mapped_fields; }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
gravity-forms/gw-user-registration-prevent-populating-mapped-fields.php(1 hunks)
🔇 Additional comments (1)
gravity-forms/gw-user-registration-prevent-populating-mapped-fields.php (1)
17-20: Good alternative solution provided.The commented-out alternative for clearing all mapped fields is a good inclusion for flexibility. Consider adding a configuration option that would allow users to toggle between clearing specific fields and clearing all fields.
saifsultanc
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good. You could maybe add an "instructions" block, minimalistic like this can also be handy for future customers : https://github.com/gravitywiz/snippet-library/blob/master/gravity-forms/display-number-of-entries-left.php#L8-L12
Overall, LGTM!
…snippet to prevent mapped fields from being populated with saved values from the User Registration feed.
b859487 to
2a3a079
Compare
|
@saifsultanc I've added instructions block. Should I do S&M? |
Yes! |
Context
⛑️ Ticket(s): https://secure.helpscout.net/conversation/2875771201/79417
Summary
This snippet prevents mapped fields from being populated with saved values from the User Registration feed.